package manegement;

import java.util.HashMap;
import java.util.Iterator;

public class BlogPathTree {


	private HashMap<String, String> blogUser;
	private HashMap<String, String> postBlog;
	private HashMap<String, String> annoucementUser;
	private HashMap<String, String> mediaPost;
	private HashMap<String, String> commentPost;


//TODO Construtor a partir do DBFacade


	public BlogPath getBlogPath(String blogId){
		return new BlogPath(blogUser.get(blogId), null, null);
	}

	public BlogPath getAnnoucementPath(String annoucementId){
		return new BlogPath(annoucementUser.get(annoucementId), null, null);
	}

	public BlogPath getPostPath(String postId){
		String blogId = postBlog.get(postId);
		return new BlogPath(blogUser.get(blogId), blogId, null);
	}
	public BlogPath getCommentPath(String commentId){
		String postId = commentPost.get(commentId);
		String blogId = postBlog.get(postId);

		return new BlogPath(blogUser.get(blogId), blogId, postId);
	}

	public BlogPath getMediaPath(String mediaId){
		String postId = mediaPost.get(mediaId);
		String blogId = postBlog.get(postId);
		return new BlogPath(blogUser.get(blogId), blogId, postId);
	}

	public void addBlogId(String userId, String newBlogId){
		blogUser.put(newBlogId, userId);
	}

	public void addAnnoucementId(String userId, String newAnnoucementId){
		annoucementUser.put(newAnnoucementId, userId);
	}

	public void addPostId(String blogId, String newPostId){
		postBlog.put(newPostId, blogId);
	}

	public void addMediaId(String postId, String newMediaId){
		mediaPost.put(newMediaId, postId);
	}


	public void addCommentId(String postId, String newCommentId){
		commentPost.put(newCommentId, postId);
	}

	public void deleteUserId(String userId){
		for (Iterator<String> iterator = annoucementUser.keySet().iterator(); iterator.hasNext();) {
			String annoucementId = iterator.next();
			if(annoucementUser.get(annoucementId).equals(userId))
				deleteAnnoucementId(annoucementId);
		}

		for (Iterator<String> iterator = blogUser.keySet().iterator(); iterator.hasNext();) {
			String blogId = iterator.next();
			if(blogUser.get(blogId).equals(userId))
				deleteBlogId(blogId);
		}
	}

	public void deleteBlogId(String blogId){
		for (Iterator<String> iterator = postBlog.keySet().iterator(); iterator.hasNext();) {
			String postId = iterator.next();
			if(commentPost.get(postId).equals(blogId))
				deleteCommentId(postId);
		}

		blogUser.remove(blogId);
	}

	public void deleteAnnoucementId(String annoucementId){
		annoucementUser.remove(annoucementId);
	}

	public void deletePostId(String postId){
		for (Iterator<String> iterator = commentPost.keySet().iterator(); iterator.hasNext();) {
			String commentId = iterator.next();
			if(commentPost.get(commentId).equals(postId))
				deleteCommentId(commentId);
		}

		for (Iterator<String> iterator = mediaPost.keySet().iterator(); iterator.hasNext();) {
			String mediaId = iterator.next();
			if(mediaPost.get(mediaId).equals(postId))
				deleteMediaId(mediaId);
		}

		postBlog.remove(postId);

	}

	public void deleteMediaId(String mediaId){
		mediaPost.remove(mediaId);
	}


	public void deleteCommentId(String commentId){
		commentPost.remove(commentId);			

	}
}


